package com.leilei.config;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.fastjson.JSON;
import com.leilei.entity.Result;
import lombok.extern.log4j.Log4j2;

/**
 * @author lei
 * @create 2022-04-21 17:04
 * @desc 限流处理
 **/
@Log4j2
public class FlowBlockClass {

    public static Result blockHandler(BlockException exception) {
        if (exception instanceof FlowException) {
            FlowException e = (FlowException) exception;
            FlowRule rule = e.getRule();
            log.warn("接口资源:{}已被限流", rule.getRefResource());
            return Result.fail("限流异常!限流配置为:" + JSON.toJSONString(rule));
        }
        return Result.fail();
    }
}
